package cn.yeyiu.netonlinevideo.parser.manga;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.parser.Parser;
import cn.yeyiu.netonlinevideo.util.AESUtil;
import cn.yeyiu.netonlinevideo.viewmodel.DataListViewModel;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.bean.MangaEP;
import cn.yeyiu.util.bean.MangaDetail;
import cn.yeyiu.util.bean.MediaData;

public class AlphaMangaParser extends Parser {
    private final String AES_PASSWD = "625222f9149e961d";
    private final String AES_IV = "5efdtf6060e2o330";

    public AlphaMangaParser(SourceData source) {
        super(source);
    }

    @Override
    public Object getBaseData(String type, int pageIndex) {
        String vtype = getArgv(type, "vtype");
        String para = AESUtil.encryptHex(
                String.format(Locale.CHINA, "{\"page\":%d}", pageIndex+1),
                AES_PASSWD, AES_IV, AESUtil.AL_CBC_5P
        );
        // para = "896deb2e05b1a7390959090669feefc0";
        // params=aes {"page":1}
        String url = source.url + String.format("%s?params=%s", vtype, para);
        String data = NetUtil.getByUrl(url);

        data = AESUtil.decryptHex(
                data, AES_PASSWD, AES_IV, AESUtil.AL_CBC_5P
        );
        return JSONObject.parseObject(data);
    }

    @Override
    public List<MediaData> getDataList(Object data, String type) {
        JSONObject jo = (JSONObject) data;
        JSONArray array = jo.getJSONObject("data").getJSONArray("data");

        //{"data":{"data":[
        // {"id":1505,
        // "title":"\u88ab\u72d9\u51fb\u7684\u6deb\u81ed\u719f\u6bcd \u545c\u54bd\u54ed\u6ce3\u7684\u9ad8\u6f6e\u814c\u6e0d",
        // "thumb":"https:\/\/de-comics.xmmwtx.com\/uploads\/comics\/2019-10\/20\/0000.jpg","
        // descriptions":""},
        // {"id":2554,"title":"\u6b66\u5668\u5c4b\u306e\u5ac1 \u62d8\u675f\u51cc\u8fb1","thumb":"https:\/\/de-comics.xmmwtx.com\/uploads\/comics\/20211201\/764\/01.jpg","descriptions":""},{"id":2552,"title":"\u3046\u3057\u304a\u306e\u3060\u304d\u3054\u3053\u3061","thumb":"https:\/\/de-comics.xmmwtx.com\/uploads\/comics\/20211201\/763\/00.jpg","descriptions":""},{"id":2551,"title":"\u4eca\u6cc9\u3093\u5bb6\u306f\u3069\u3046\u3084\u3089\u30ae\u30e3\u30eb\u306e\u6e9c\u307e\u308a\u5834\u306b\u306a\u3063\u3066\u308b\u3089\u3057\u3044","thumb":"https:\/\/de-comics.xmmwtx.com\/uploads\/comics\/20211201\/762\/002.jpg","descriptions":""},{"id":2550,"title":"\u8def\u5730\u88cf\u306e\u30a8\u30eb\u30aa","thumb":"https:\/\/de-comics.xmmwtx.com\/uploads\/comics\/20211201\/761\/01.jpg","descriptions":""},{"id":2549,"title":"\u611b\u3057\uff5e\u30d7\u30ec\u30a4","thumb":"https:\/\/de-comics.xmmwtx.com\/uploads\/comics\/20211201\/760\/01.jpg","descriptions":""},{"id":2548,"title":"\u50ac\u7720\u306e\u738b\u69d8\uff5e\u8ab0\u3082\u304c\u4ffa\u306b\u3072\u3056\u307e\u305a\u304f\u795e\u306e\u529b\u3092\u624b\u306b\u5165\u308c\u305f","thumb":"https:\/\/de-comics.xmmwtx.com\/uploads\/comics\/20211201\/759\/01.jpg","descriptions":""},{"id":2547,"title":"\u8056\u5973\u306e\u30e4\u30ea\u90e8\u5c4b","thumb":"https:\/\/de-comics.xmmwtx.com\/uploads\/comics\/20211201\/758\/01.jpg","descriptions":""},{"id":2546,"title":"NIPPON WONDER WIFE \u30ad\u30f3\u30b0\u30c9\u30df\u30cd\u30fc\u30bf\u30fc\u7de8","thumb":"https:\/\/de-comics.xmmwtx.com\/uploads\/comics\/20211201\/757\/nipponwonderwife_001.jpg","descriptions":""},{"id":2545,"title":"\u3044\u3051\u306a\u3044\u304a\u533b\u8005\u3055\u3093\u3054\u3063\u3053","thumb":"https:\/\/de-comics.xmmwtx.com\/20211201\/756\/01.jpg","descriptions":""},{"id":2544,"title":"\u3046\u3089\u3089\u306e\u307e\u304f\u3089","thumb":"https:\/\/de-comics.xmmwtx.com\/20211201\/755\/01.jpg","descriptions":""},{"id":2543,"title":"\u6d44\u7f6a\u306e\u5148\u306b\u3042\u308b\u3082\u306e\u306f","thumb":"https:\/\/de-comics.xmmwtx.com\/uploads\/comics\/20211201\/754\/01.jpg","descriptions":""}],"total_page":0},"code":1,"message":"\u67e5\u8be2\u6210\u529f"}
        List<MediaData> videoInfos = new ArrayList<>();
        MediaData mediaData;
        for (int i = 0; i < array.size(); ++i) {
            JSONObject ob = array.getJSONObject(i);
            mediaData = new MediaData();

            mediaData.id = ob.getString("id");
            mediaData.code = source.code;
            mediaData.title = ob.getString("title");
            mediaData.logo = ob.getString("thumb");
            videoInfos.add(mediaData);
        }
        return videoInfos;
    }

    @Override
    public MangaDetail buildData(MediaData mediaData){
        //{"id":"2496","page":1,"sort":1}
        String para = AESUtil.encryptHex(
                String.format(Locale.CHINA, "{\"id\":\"%s\",\"page\":1,\"sort\":1}", mediaData.id),
                AES_PASSWD, AES_IV, AESUtil.AL_CBC_5P
        );
        String url = source.url+ String.format("lists?params=%s", para);
        String data = NetUtil.getByUrl(url);

        data = AESUtil.decryptHex(
                data,AES_PASSWD, AES_IV, AESUtil.AL_CBC_5P
        );
        JSONArray array = JSONObject.parseObject(data).getJSONObject("data").getJSONArray("data");
        //[{"id":23258,
        // "name":"\u88ab\u72d9\u51fb\u7684\u6deb\u81ed\u719f\u6bcd \u545c\u54bd\u54ed\u6ce3\u7684\u9ad8\u6f6e\u814c\u6e0d-1",
        // "praises":318,
        // "browses":0,
        // "image":"https:\/\/de-comics.xmmwtx.com\/uploads\/comics\/2019-10\/20\/0001.jpg","liked":0,"is_praise":false},
        MangaDetail mangaDetail = new MangaDetail();
        mangaDetail.setTitle(mediaData.title);
        mangaDetail.setLogo(mediaData.logo);

        List<MangaEP> mangaEPs = new ArrayList<>();
        MangaEP mangaEP;
        for (int i = 0; i < array.size(); ++i) {
            JSONObject ob = array.getJSONObject(i);
            mangaEP = new MangaEP();

            mangaEP.setTitle(ob.getString("name"));
            mangaEP.setLogo(ob.getString("image"));
//            mangaEP.setCount(ob.getIntValue("praises"));
            String list_id = ob.getString("id");
            mangaEP.picList = new ArrayList<>();

            para = AESUtil.encryptHex(
                    String.format(Locale.CHINA, "{\"list_id\":\"%s\"}", list_id),
                    AES_PASSWD, AES_IV, AESUtil.AL_CBC_5P
            );
            url = source.url+ String.format("watch?params=%s", para);
            data = NetUtil.getByUrl(url);
            data = AESUtil.decryptHex(
                    data,AES_PASSWD, AES_IV, AESUtil.AL_CBC_5P
            );
            JSONArray arrayPic = JSONObject.parseObject(data).getJSONArray("data");
            for (int j = 0; j < arrayPic.size(); ++j) {
                mangaEP.picList.add(arrayPic.getJSONObject(j).getString("image"));
            }

            mangaEPs.add(mangaEP);
        }
        mangaDetail.setEps(mangaEPs);
        return mangaDetail;
    }
}
